﻿using System;
using System.Collections.Generic;
using System.Collections;

using System.Web;
using System.IO;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;

/// <summary>
/// Summary description for utility
/// </summary>
public class utility
{
	public utility()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static void log(string mensaje) {
        string ruta = "c:\\TEMP\\rrhh_"+DateTime.Now.Day+"_"+DateTime.Now.Month+"_"+DateTime.Now.Year+".txt";
        StreamWriter escritor = new StreamWriter(ruta, true);
        escritor.WriteLine(DateTime.Now.ToShortTimeString() + " --- " + mensaje);
        escritor.Close();
        return;
    }

    public static DataTable getDataTableCuenta(ArrayList arr)
    {
        DataTable dt = new DataTable("cuenta");
        dt.Columns.Add("name");
        dt.Columns.Add("manager");
        dt.Columns.Add("description");

        try {
            if ((arr != null) && (arr.Count > 0)) {
                foreach (RE_GenericBean rgb in arr) {
                    object[] objArr = { rgb.strC1, rgb.strC2, rgb.strC3 };
                    dt.Rows.Add(objArr);
                }
            }
        } catch (Exception err) {
            log(err.StackTrace);
            return null;
        }
        return dt;
    }

    //para generar xml de validacion de usuario
    public static string setXml(string user, string pass, int app, int perfilid)
    {
        string result = "";

        result = "<?xml version=\"1.0\" encoding=\"utf-8\"?><datos_login>";
        result = result + "<usuarioid>";
        result = result + user;
        result = result + "</usuarioid> <contrasena>" + pass;
        result = result + "</contrasena><aplicacionid>" + app + "</aplicacionid><perfilid>" + perfilid + "</perfilid></datos_login>";
        return result;


    }

    public static RE_GenericBean getUserLogin(string datos)
    {
        RE_GenericBean result = new RE_GenericBean();


        try
        {
            XmlDocument xDoc = new XmlDocument();

            xDoc.Load(new StringReader(datos));

            XmlElement datosrespuesta = xDoc.DocumentElement;

            foreach (XmlElement nodo in datosrespuesta)
            {
                if (nodo.Name.Equals("respuestaid"))
                {

                    int rid = int.Parse(nodo.InnerText);
                    result.intC1 = rid;
                }
                if (nodo.Name.Equals("usuarioid"))
                {

                    result.strC1 = nodo.InnerText;
                }
                if (nodo.Name.Equals("pass"))
                {

                    result.strC2 = nodo.InnerText;
                }
                if (nodo.Name.Equals("nombre"))
                {

                    result.strC3 = nodo.InnerText;
                }
                if (nodo.Name.Equals("apellido"))
                {

                    result.strC4 = nodo.InnerText;
                }
                if (nodo.Name.Equals("alias"))
                {

                    result.strC5 = nodo.InnerText;
                }
                if (nodo.Name.Equals("perfilid"))
                {
                    result.strC6 = nodo.InnerText;
                }

                
            }
        }
        catch (Exception ex)
        {
            result = null;
        }

        return result;
    }

    public static DataTable getDataTableEmpleadoBancoCuenta(ArrayList arr)
    {
        DataTable dt = new DataTable("cuenta");
        dt.Columns.Add("ID");
        dt.Columns.Add("Account#");
        dt.Columns.Add("Type Account");
        dt.Columns.Add("Empleado");
        dt.Columns.Add("Status");
        dt.Columns.Add("Bank");

        try
        {
            if ((arr != null) && (arr.Count > 0))
            {
                foreach (RE_GenericBean rgb in arr)
                {
                    object[] objArr = { rgb.intC1, rgb.strC1, rgb.strC2, rgb.intC2, rgb.intC3, rgb.intC4 };
                    dt.Rows.Add(objArr);
                }
            }
        }
        catch (Exception err)
        {
            log(err.StackTrace);
            return null;
        }
        return dt;
    }

    public static DataTable getDataTableBancos(ArrayList arr)
    {
        DataTable dt = new DataTable("banco");
        dt.Columns.Add("id");
        dt.Columns.Add("nombre");
        dt.Columns.Add("descripcion");
        dt.Rows.Add(0,"Select One","");

        try
        {
            if ((arr != null) && (arr.Count > 0))
            {
                foreach (RE_GenericBean rgb in arr)
                {
                    object[] objArr = { rgb.intC1, rgb.strC1, rgb.strC2 };
                    dt.Rows.Add(objArr);
                }
            }
        }
        catch (Exception err)
        {
            log(err.StackTrace);
            return null;
        }
        return dt;
    }
    public static DataTable getDataTableDocuments(ArrayList arr)
    {
        DataTable dt = new DataTable("document");
        dt.Columns.Add("id");
        dt.Columns.Add("nombre");
        dt.Columns.Add("obligatorio");
        dt.Columns.Add("visible");
        dt.Columns.Add("path");
        dt.Columns.Add("doc_id");
       
        try
        {
            if ((arr != null) && (arr.Count > 0))
            {
                foreach (RE_GenericBean rgb in arr)
                {
                    if (rgb.intC3 == 1)
                    {
                        object[] objArr = { rgb.intC1, rgb.strC1, rgb.intC2, rgb.intC3, rgb.strC2, rgb.intC4 };
                        dt.Rows.Add(objArr);
                    }
                }
            }
        }
        catch (Exception err)
        {
            log(err.StackTrace);
            return null;
        }
        return dt;
    }

    public static RE_GenericBean  getControl(int country, string tipocontrol, string nombrecontrol)
    {
        RE_GenericBean result= new RE_GenericBean();


        if (tipocontrol.Equals("label")) {
            result.strC1 = DB.getTextoControl(country, nombrecontrol);
        }
        if (tipocontrol.Equals("textbox"))
        {
            result.strC1  = DB.getContenidoControl(country, nombrecontrol);
            result.intC1 = DB.getMaxLongitudControl(country, nombrecontrol);
        }
        if (tipocontrol.Equals("exp_regular"))
        {
            result.strC1 = DB.getTextoControl(country, nombrecontrol);
            result.strC2 = DB.getContenidoControl(country, nombrecontrol);
        }
       

        return result;
    }

    



    public static DataTable getControlDDL(int country, string tipocontrol, string nombrecontrol)
    {
        DataTable result = null;


       
        if (tipocontrol.Equals("ddl"))
        {
            result = DB.getOptionsControl(country, nombrecontrol);
        }


        return result;
    }

    public static bool isCompletedProfile(int p, int compid, int tipo)
    {
        bool result = true;

        RE_GenericBean personainf = DB.getPersonaInf(p, 0);
        ArrayList contactarr = (ArrayList)DB.getContactArr("where tpd_tper_id=" + p);
        ArrayList emergencyarr = (ArrayList)DB.getEmergencyArr("where tpem_tper_id=" + p);
        ArrayList expedientearr = (ArrayList)DB.getExpedienteArr(int.Parse(personainf.intC2.ToString()));
        ArrayList bancaccarr = (ArrayList)DB.getArrEmpleadoCuentaBanco(" where emp_accbank_empid=" + personainf.intC2  + "and emp_accbank_status=1");
        ArrayList docarr = (ArrayList)DB.getArrDocumentbyEmp(compid, tipo, personainf.intC2);
        if (personainf != null)
        {



            if (contactarr == null || contactarr.Count == 0) { result = result & false; }
            else
            {
                if (emergencyarr == null || emergencyarr.Count == 0) { result = result & false; }
                else {
                    if (expedientearr == null || expedientearr.Count == 0) { result = result & false; }
                    else {
                        result= result &  validartabs(personainf, contactarr, emergencyarr, expedientearr);
                        if (docarr != null ) {
                           
                            result = result & validarDocs(docarr,1);
                        }
                        if (bancaccarr == null || bancaccarr.Count == 0) { result = result & false; }
                    }
                }
            }
        }
        else {
            result = false;
        
        }

        return result;
    }

    private static bool validarDocs(ArrayList docarr, int type)
    {
        bool result= true;

        foreach (RE_GenericBean re in docarr)
        {
            if (re.intC2 == 1 && re.strC2.Equals("")) {
                result = result & false;                
            }
            
        }



        return result;
        
    }

    private static bool validartabs(RE_GenericBean personainf, ArrayList contactarr, ArrayList emergencyarr, ArrayList expedientearr)
    {
        bool result = true;
        //validando tab1 (personal detail)
        if (personainf.intC1 == 0 || personainf.strC1.Equals("")  || personainf.strC3.Equals("") || personainf.strC4.Equals("") || personainf.strC6.Equals("")
              || personainf.strC7.Equals("") || personainf.strC8.Equals(""))
        {
            result = result & false;
        }



        //validanto tab2 (affilliation)
        if (personainf.strC14.Equals("") || personainf.intC5 == 0 || personainf.strC9.Equals("") || personainf.strC10.Equals("") || personainf.strC16.Equals(""))
        {
            result = result & false;
        }


        //validando tab3 (emergency contacts)
        int cont = 0;
        foreach (RE_GenericBean rg in emergencyarr) {
            
            if (cont < 2) {

                if (rg.strC1.Equals("") || rg.strC2.Equals("") || rg.strC8.Equals("") || rg.strC9.Equals("")) {
                    result = result & false;
                }
            }
            cont++;
        }

        //validando tab4 (jobposition)
        if (personainf.intC4 == 0 || personainf.strC12.Equals("")  || personainf.strC11.Equals("")) {
            result = result & false;
        
        }


        //validando tab5  (salary)
        foreach (RE_GenericBean r in expedientearr){
             
            if (r.strC1.Equals("") || r.decC1 == 0 ||  r.decC2 == 0 || r.decC5 == 0 
                || r.strC2.Equals("")){
                    result = result & false;
            }   
        
        }


        return result;
    }
    public static DropDownList fillDDLCompanies(DropDownList ddl) {

        ddl.Items.Clear();
        ArrayList companyarr = null;
        ListItem item = null;
        item = new ListItem("Select One", "0");
        ddl.Items.Add(item);
        try
        {
            companyarr = (ArrayList)DB.getArrCompania("");
            if (companyarr != null && companyarr.Count > 0)
            {
                foreach (RE_GenericBean company in companyarr)
                {
                    item = new ListItem(company.strC1, company.intC1.ToString());
                    ddl.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
            return ddl;
        }
        return ddl;
    
    }

    public static DataTable getDataTableEmployee(ArrayList arr)
    {
        DataTable dt = new DataTable("employee");
        dt.Columns.Add("per_id");
        dt.Columns.Add("First Name");
        dt.Columns.Add("Middle Name");
        dt.Columns.Add("Last Name");
        dt.Columns.Add("Batch");
        dt.Columns.Add("Account");
        dt.Columns.Add("Job Title");
        dt.Columns.Add("Status");
   

        try
        {
            if ((arr != null) && (arr.Count > 0))
            {
                foreach (RE_GenericBean rgb in arr)
                {
                    object[] objArr = { rgb.intC1, rgb.strC1, rgb.strC2, rgb.strC3, rgb.strC4, rgb.strC5, rgb.strC6, rgb.intC2 };
                    dt.Rows.Add(objArr);
                }
            }
        }
        catch (Exception err)
        {
            log(err.StackTrace);
            return null;
        }
        return dt;
    }
}